package com.pray.rabbitmq.listener;

import com.pray.entity.po.Account;
import com.pray.entity.po.Role;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * TestListener
 * 监听器，实际上作为消费者
 * @author 春江花朝秋月夜
 * @since 2023/11/10 0:12
 */
@Component  //注册为Bean,作为接受消息同时处理返回的监听器
public class TestListener {
    //@RabbitListener直接声明交换机和队列的绑定关系
    @RabbitListener(queues = "fanout.queue1",containerFactory = "listenerContainer",messageConverter = "Jackson2JsonMessageConverter")
    public void fanoutListen1(Message message){
        System.out.println(message);
    }
    @RabbitListener(queues = "fanout.queue2",containerFactory = "listenerContainer",messageConverter = "Jackson2JsonMessageConverter")
    public void fanoutListen2(Role role){
        System.out.println(role);
    }
    @RabbitListener(bindings = @QueueBinding(
          value = @Queue(name = "direct.queue1"),
          exchange = @Exchange(name = "direct.exchange",
                  type = ExchangeTypes.DIRECT),
          key = {"queue1","queue1Exchange"}
    ),messageConverter = "Jackson2JsonMessageConverter")
    public void directListener1(Message msg){
        System.out.println("queue1: "+msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "direct.exchange",
                    type = ExchangeTypes.DIRECT),
            key = {"queue2","queue2Exchange"}
    ),messageConverter = "Jackson2JsonMessageConverter")
    public void directListener2(Message msg){
        System.out.println("queue2: "+msg);
    }
    //主题模式，通过通配符来实现路由简化绑定关系
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trace.queue1"),
            exchange = @Exchange(name = "trace.exchange",
                    type = ExchangeTypes.TOPIC),
            key = {"*.like"}
    ),messageConverter = "Jackson2JsonMessageConverter")
    public void traceListener1(Message msg){
        System.out.println("trace:queue1: "+msg);
    }
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trace.queue2"),
            exchange = @Exchange(name = "trace.exchange",
                    type = ExchangeTypes.TOPIC),
            key = {"*.bike"}
    ),messageConverter = "Jackson2JsonMessageConverter")
    public void traceListener2(Message msg){
        System.out.println("trace:queue2: "+msg);
    }
}
